<?php

class NivoSliderBlock extends BlockComponent {
    public $id = 'nivoslider';

    public $width;
    public $height;

    public $autoResize = true;

    /**
     * attribute
     * @var array
     */
    public $images = array();

    public function run(){
        //read from cache
        $cache = app()->cache->get('nivoslider_cache_'.$this->block->block_id);
        if(!$cache){
            parent::run();

            //register js
            $cs = Yii::app()->clientScript;
            $cs->registerCoreScript('jquery');
            //publish assets folder
            $assetFolder = Yii::app()->assetManager->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets');
            $cs->registerCssFile($assetFolder . '/nivo-slider.css');
            $cs->registerScriptFile($assetFolder. '/jquery.nivo.slider.js');
        }
        
        $this->render($this->outputView);
    }

    /**
     * get class attributes
     * @return type
     */
    public function params() {
        return array(
            'images' => 'Images array',
            'width' => 'Width',
            'height' => 'Height',
            'autoResize' => 'Auto resize'
        );
    }

    /**
     * action before save and validate
     */
    public function beforeBlockSave() {
        if(r()->isPostRequest){
            //need reset
            $this->images = array();

            //save photo
            if(is_array($_POST['image'])){
                foreach($_POST['image'] as $key => $image){
                    if(($newImage = CUploadedFile::getInstanceByName('image['.$key.'][newimage]'))){
                        //save new image
                        $newFileName = uniqid().'.'.$newImage->getExtensionName();
                        if($newImage->saveAs(UPLOAD_PATH . $newFileName)){
                            //adding to images
                            $this->images[] = array(
                                'filePath' => $newFileName,
                                'title' => $image['title'],
                                'description' => $image['description'],
                                'url' => $image['url']
                            );
                        }
                    }else{
                        if($image['oldimage'] != ''){
                            //
                            $this->images[] = array(
                                'filePath' => $image['oldimage'],
                                'title' => $image['title'],
                                'description' => $image['description'],
                                'url' => $image['url']
                            );
                        }
                    }
                }

                //dont forget this line
                $_POST['Block']['images'] = $this->images;
            }
        }
    }

    public function validate() {
        if (empty($this->images)) {
            $this->errors['images'] = 'Images required';
            return false;
        }
        else
            return true;
    }

}